package com.example.volleytest.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.android.volley.toolbox.NetworkImageView;
import com.example.volleytest.R;
import com.example.volleytest.network.VolleyHelper;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 14.07.13
 * Time: 11:01
 * To change this template use File | Settings | File Templates.
 */
public class AvatarsAdapter extends BaseAdapter {
    private static final String AVATAR_REQUEST_TAG = "avatar_tag";
    private static final String[] URLS = {
            "http://cs317916.vk.me/v317916101/6312/w5CViGr1zJo.jpg",
            "http://cs320716.vk.me/v320716118/1220/dg2NOMXkfss.jpg",
            "http://cs4148.vk.me/u55017047/d_28505fbf.jpg",
            "http://cs417628.vk.me/v417628300/ae49/5U3PD7sKRRg.jpg",
            "http://cs14.vk.me/u29195/d_d86139ae.jpg",
            "http://cs734.vk.me/u10384584/d_6d6d283b.jpg",
            "http://cs315624.vk.me/v315624369/3b9c/39xrYe0tszc.jpg",
            "http://cs402519.vk.me/v402519295/5e0a/N-VzNwsndSc.jpg",
            "http://cs305401.vk.me/v305401417/6216/duq6-s-IoNI.jpg",
            "http://cs419122.vk.me/v419122536/1a37/ApbpXF_px_k.jpg",
            "http://cs315624.vk.me/v315624369/3b9c/39xrYe0tszc.jpg",
            "http://cs402519.vk.me/v402519295/5e0a/N-VzNwsndSc.jpg",
            "http://cs305401.vk.me/v305401417/6216/duq6-s-IoNI.jpg",
            "http://cs419122.vk.me/v419122536/1a37/ApbpXF_px_k.jpg",
            "http://cs425217.vk.me/v425217265/138b/x_NiV5h9zMo.jpg",
            "http://cs409024.vk.me/v409024396/1a7b/5aIkXkO3SRs.jpg",
            "http://cs310731.vk.me/v310731206/1f34/i-7QcntPzek.jpg",
            "http://cs319924.vk.me/v319924957/7f0c/hB1a-cFyays.jpg",
            "http://cs643.vk.me/u41623786/d_6f9526fd.jpg",
            "http://cs9666.vk.me/u16690475/d_c82548fb.jpg",
            "http://cs416131.vk.me/v416131825/73e1/L0pV845tVRk.jpg",
            "http://cs4576.vk.me/u1156727/d_e17a83ab.jpg",
            "http://cs317723.vk.me/u6905653/d_ba07a068.jpg",
            "http://cs421323.vk.me/v421323140/5b97/4THzlUnnPdI.jpg",
            "http://cs416131.vk.me/v416131825/73e1/L0pV845tVRk.jpg",
            "http://cs4576.vk.me/u1156727/d_e17a83ab.jpg",
            "http://cs317723.vk.me/u6905653/d_ba07a068.jpg",
            "http://cs421323.vk.me/v421323140/5b97/4THzlUnnPdI.jpg",
            "http://cs403921.vk.me/v403921218/460d/YYJ-luNQI1Y.jpg",
            "http://cs410417.vk.me/v410417054/b357/0-bp1MGxpIo.jpg",
            "http://cs417016.vk.me/v417016586/551f/L2Ea14EXkes.jpg",
            "http://cs230.vk.me/u2037683/d_d123456f.jpg",
            "http://cs421629.vk.me/v421629403/273b/1mvYKhLBNeU.jpg",
            "http://cs9268.vk.me/v9268608/cd9/TCfpqK-0Q4M.jpg",
            "http://cs11469.vk.me/u122344662/d_574bf468.jpg",
            "http://cs709.vk.me/u556237/d_f7e7170f.jpg",
            "http://cs9964.vk.me/u21475828/d_699113c5.jpg",
            "http://cs316719.vk.me/v316719150/6f80/bxLxLGEw3Pk.jpg",
            "http://cs11469.vk.me/u122344662/d_574bf468.jpg",
            "http://cs709.vk.me/u556237/d_f7e7170f.jpg",
            "http://cs9964.vk.me/u21475828/d_699113c5.jpg",
            "http://cs316719.vk.me/v316719150/6f80/bxLxLGEw3Pk.jpg",
            "http://cs308231.vk.me/u179082346/d_69fe0291.jpg",
            "http://cs9834.vk.me/v9834239/3b/YiGoND0R9kE.jpg",
            "http://cs232.vk.me/u39819845/d_7de92f5c.jpg",
            "http://cs417418.vk.me/v417418387/5e5d/sY1b11X-TpI.jpg",
            "http://cs9646.vk.me/u13742270/d_69f98d9e.jpg",
            "http://cs418922.vk.me/v418922275/5d79/0lK47xq5lPU.jpg",
            "http://cs4138.vk.me/u2146778/d_c6d0b94a.jpg",
            "http://cs408117.vk.me/v408117503/2f91/zIWXe0b6fzg.jpg",
            "http://cs10532.vk.me/u128601/d_1746a284.jpg",
            "http://cs412328.vk.me/v412328014/2864/vJ74hHLOdjc.jpg",
            "http://cs4138.vk.me/u2146778/d_c6d0b94a.jpg",
            "http://cs408117.vk.me/v408117503/2f91/zIWXe0b6fzg.jpg",
            "http://cs10532.vk.me/u128601/d_1746a284.jpg",
            "http://cs412328.vk.me/v412328014/2864/vJ74hHLOdjc.jpg" };

    private LayoutInflater layoutInflater;

    public AvatarsAdapter(LayoutInflater layoutInflater) {
        super();
        this.layoutInflater = layoutInflater;
    }

    @Override
    public int getCount() {
        return URLS.length;
    }

    @Override
    public Object getItem(int i) {
        return URLS[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = layoutInflater.inflate(R.layout.avatars_list_item, null);

            ViewHolder holder = new ViewHolder();
            holder.avatar = (NetworkImageView)view.findViewById(R.id.avatar_iv);
            holder.avatar.setDefaultImageResId(R.drawable.default_avatar);

            view.setTag(holder);
        }

        ViewHolder holder = (ViewHolder)view.getTag();
        holder.avatar.setImageUrl(URLS[i], VolleyHelper.getInstance().getImageLoader());

        return view;
    }

    public void cancelRequests() {
        VolleyHelper.getInstance().cancelAll(AVATAR_REQUEST_TAG);
    }

    private static class ViewHolder {
        public NetworkImageView avatar;
    }

}
